<?php

	function file_image_save($file , $filename , $param)
	{
		$rnt = array() ;
		if (count($param) < 1){
			$rnt['result'] = 0 ;
			$rnt['message'] = "参数错误." ;
			return $rnt ;
		}
		
		$cmd 	= $param[0] ;
		if (count($param) < 3){
			$rnt['result'] = 0 ;
			$rnt['message'] = "参数错误." ;
			return $rnt ;
		}
		if ($cmd == "param_vr"){			
			$bg_img_id =  $param[1]  ;
			$vr_buff_id = $param[2]  ;
			$save_name = $param[3]  ;
			$feild = "" ;
			
			$fne = explode(".",$filename) ;
			if (count($fne) >= 2){
				$nm = explode("_", $fne[count($fne) - 2]) ;
				$vl = $nm[count($nm) - 1] ;
				
				if ($vl == "b")		$feild = "img_b" ;
				if ($vl == "d")		$feild = "img_d" ;
				if ($vl == "f")		$feild = "img_f" ;
				if ($vl == "l")		$feild = "img_l" ;
				if ($vl == "r")		$feild = "img_r" ;
				if ($vl == "u")		$feild = "img_u" ;
				
				if ($feild == ""){
					$rnt['result'] = 0 ;
				 	$rnt['message'] = "文件名格式错误" ;
				 	return $rnt ;
				}
			}else{
				$rnt['result'] = 0 ;
			 	$rnt['message'] = "文件名格式错误" ;
			 	return $rnt ;
			}
			
			$img_one = sql_fetch_one("select * from sys_image where `id` = $bg_img_id") ;
			$vr_buff = sql_fetch_one("select * from taozhuang_vr_buffer where `id` = '$vr_buff_id'") ;
			
			$addr  = $img_one['url'] ;
			$nm = explode(".", $addr) ;
			
			$path = $nm[0] . "/" . $save_name."_".$feild.".jpg" ;
			$save_path_over = $GLOBALS['upload_Path'] . $path ;
	
			make_dir(getPath_dir($save_path_over)) ;
			if (move_uploaded_file($file, $save_path_over)){
				$size = getimagesize($save_path_over) ;
				$img_width = $size[0] ;
				$img_height = $size[1] ;
				
				if (intval($vr_buff[$feild]) > 0){
					// 覆盖已有
					$newid = intval($vr_buff[$feild]) ;
					if (intval(sql_fetch_one_cell("select `id` from `sys_image` where `id` = '$newid'")) == $newid){
						sql_query("update `sys_image` set `url` = '$path', `update_time` = unix_timestamp() , `type` = '{$img_one['type']}' , `name` = '$filename'  , `width` = '$img_width' , `height` = '$img_height' where `id` = $newid") ;
					}else{
						$newid = sql_insert("insert into sys_image(`type` , `name` , `width` , `height`, `url` , `update_time`) values ('{$img_one['type']}' , '$filename' , '$img_width' , '$img_height' , '$path' , unix_timestamp())") ;
					}
				}else{
					// 重新生成
					$newid = sql_insert("insert into sys_image(`type` , `name` , `width` , `height`, `url` , `update_time`) values ('{$img_one['type']}' , '$filename' , '$img_width' , '$img_height' , '$path' , unix_timestamp())") ;
				}
				sql_query("update taozhuang_vr_buffer set `$feild` = '$newid' where `id` = '$vr_buff_id'") ;
				
				
				$rnt['result'] = 1 ;	// 成功保存文件
			 	$rnt['message'] = "VR图片保存成功.path=".$save_path_over ;
			 	$rnt['feild'] = $feild ;
			 	$rnt['value'] = $newid ;
			 	$rnt['url'] = $GLOBALS['URL_IMG_HEAD'].$path ;
			}else{
			 	$rnt['result'] = 0 ;
			 	$rnt['message'] = "VR图片保存失败." ;
			 	return $rnt ;
			}
	
			return $rnt ;
		}
		
		
		if (count($param) < 4){
			$rnt['result'] = 0 ;
			$rnt['message'] = "参数错误." ;
			return $rnt ;
		}
		$typeid = $param[1] ;
		$w 		= $param[2] ;
		$h		= $param[3] ; 
		$z		= $param[4] ;
		if ($cmd == "paramid"){
			$img_one = sql_fetch_one("select * from sys_image where `id` = $typeid") ;
			$newid = $img_one['id'] ;
			if (intval($newid) != intval($typeid)){
				$rnt['result'] = 0 ;
				$rnt['message'] = "没找到老图片." ;
				return $rnt ;
			}
			$save_path = $GLOBALS['upload_Path'] . $img_one['url'] ;
			make_dir(getPath_dir($save_path)) ;
			if (move_uploaded_file($file, $save_path)){
				$size = getimagesize($save_path) ;
				$w = $size[0] ;
				$h = $size[1] ;
			 	$rnt['result'] = 1 ;	// 成功保存文件
			 	$rnt['message'] = "文件保存成功.path=".$save_path ;
			 	sql_query("update sys_image set `width` = '$w' , `height` = '$h' where  `id` = $typeid") ;
			}else{
			 	$rnt['result'] = 0 ;
			 	$rnt['message'] = "文件替换失败.path=".$save_path ;
			}				
			return $rnt ;
		}
		
		
		$tyval = sql_fetch_one("select * from prf_type where `id` = '$typeid'") ;
		$addr 		= $tyval['addr'] ;
		
		$newid = sql_insert("insert into sys_image(`type` , `name` , `width` , `height`,`update_time`) values ('$typeid' , '$filename' , '$w' , '$h' , unix_timestamp())") ;
		
		$fne = explode(".",$filename) ;
		if (count($fne) >= 2){
			$fne = "." . $fne[count($fne) - 1] ;
		}else{
			$fne = ".mc.jpg" ;
		}
		if (intval($tyval['father']) >= 0){
			$fnaer = md5($newid . "_" . $typeid . $fne) ;
			$addr = "" ;
			$g = "" ;
			while(strlen($fnaer) > 10){
				$addr.= $g.substr($fnaer, 0 , 3) ;	$g = "/" ;
				$fnaer = substr($fnaer, 4) ;
			}
			$addr.= $g.$fnaer ;
			$path = "images/" . $addr . "_" . $typeid . $fne ;
			$save_path = $GLOBALS['upload_Path'] . $path ;
		}else{		
			$fnaer = md5($newid . "_" . $typeid . $fne) ;
			while(strlen($fnaer) > 10){
				$addr.= "/".substr($fnaer, 0 , 3) ;
				$fnaer = substr($fnaer, 4) ;
			}
			
			$path = "images/" . $addr . "/" . $newid . "_" . $typeid . $fne ;
			$save_path = $GLOBALS['upload_Path'] . $path ;
		}
				
		sql_query("update `sys_image` set `url` = '$path', `update_time` = unix_timestamp() where `id` = $newid") ;
		$upok = is_uploaded_file($file) ;
		make_dir(getPath_dir($save_path)) ;
		if (move_uploaded_file($file, $save_path)){    
		 	$rnt['result'] = 1 ;	// 成功保存文件
		 	$rnt['message'] = "文件保存成功.path=".$save_path ;
		 	
		 	if ($z == 1){
		 		file_image_save_to_zoom($save_path) ;	// 上传的图片重新保存,估计能小点.
		 		file_image_save_to_zoom($save_path , "thumb" , $addr , $path , $filename , 0.5 , $w , $h) ;		// 自动缩小图片 0.5
		 		file_image_save_to_zoom($save_path , "small" , $addr , $path , $filename , 0.25 , $w , $h) ;	// 自动缩小图片 0.25
		 	}
		}else{
			sql_query("delete from `sys_image` where `id` = $newid") ;
		 	$rnt['result'] = 0 ;
		 	$rnt['message'] = "文件保存失败,请检查php设置.".$save_path." , $upok " ;
		}
		return $rnt ;
	}
	function file_image_save_to_zoom($source_path , $newfnm = "" , $prf_type_addr="" , $sys_image_path="" , $filename = "" , $zoom = 1 , $w = 0 , $h = 0)
	{
		$newfnm = trim($newfnm) ; 
		if ($newfnm == ""){
			try{
				image_zoom_save($source_path , 1 , $source_path) ;
			}catch(Exception $e){		 		
				$time = date("Ymd H:i:s") ;	
				file_put_contents(dirname(__FILE__)."/error.txt","\r\n $time :".exceptionToString($e),FILE_APPEND);	 	
			}
			return ;
		}
		try{
			$source_dir = getPath_dir($source_path) ;
			$file_name = getPath_name($source_path) ;
			$s_dir = getPath_dir($sys_image_path) ;
			$f_name = getPath_name($sys_image_path) ;

			$ntid = intval(sql_fetch_one_cell("select `id` from `prf_type` where `addr` = '$prf_type_addr/$newfnm'")) ;
			if (is_int($ntid) == false || $ntid <= 0){
				$maxid = intval(sql_fetch_one_cell("select max(`id`) from `prf_type` where `id` < 1000000")) + 1 ;
				sql_insert("INSERT INTO `prf_type` (`id` , `typename`, `addr`) VALUES ('$maxid','$newfnm','$prf_type_addr/$newfnm')") ;
			}
			$zoompath = $source_dir."$newfnm/".$file_name ;
			$sqlsave = $s_dir."$newfnm/".$f_name ;
			image_zoom_save($source_path , $zoom , $zoompath) ;
						
			$zw = $w * $zoom ;
			$zh = $h * $zoom ;
			sql_insert("insert into sys_image(`type` , `name` , `width` , `height` , `url` , `update_time` ) values ('$ntid' , '$filename' , '$zw' , '$zh' , '$sqlsave' , unix_timestamp() )") ;
		}catch(Exception $e){		 	
			$time = date("Ymd H:i:s") ;	
			file_put_contents(dirname(__FILE__)."/error.txt","\r\n $time :".exceptionToString($e),FILE_APPEND);	 	
		}
	}
	// 把某个文件按照宽度同比例放大或缩小
	function file_image_saveas_zoom($img , $neww)
	{
		if (is_array($img) == false){
			$item = sql_fetch_one("select * from sys_image where `id` = '$img'") ;
		}else{
			$item = $img ;
		}
		if (is_array($item) && array_key_exists("url" , $item)){
			$file_path = $GLOBALS['upload_Path'] . $item['url'] ;
			$size = image_zoom_save($file_path , 0 , $file_path , $neww) ;
			$zw = $size[0] ;
			$zh = $size[1] ;
			sql_query("update sys_image set `width` = '$zw' , `height` = '$zh' where `id` = '{$item['id']}'") ;
			
			return 1 ;
		}
		return 0 ;
	}
?>